package com.om39a.plm.crawler;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.apache.log4j.Logger;

import com.om39a.plm.exceptions.PageLoadFailedException;
import com.om39a.plm.utils.PLMConstants;

public class BrowseLoginPage extends PLMBrowser {

	static Logger logger = Logger.getLogger(BrowseLoginPage.class);
	
	private int[] randonTimeInMills = new int[] { 2000, 3000, 4000, 5000, 6000,
			7000, 8000, 9000, 10000, 11000, 12000 };
	
	private static Random r = new Random();

	public String execute(String url, Map<String, String> args)
			throws PageLoadFailedException {

		logger.info("Exiting from execute - " + url + " - " + args);
		try {
			Thread.sleep(randonTimeInMills[r.nextInt(10)]);
		} catch (InterruptedException e) {
			e.printStackTrace();
			logger.info(e);
		}

		// Trying to login in to the page
		String loginURL = url;
		String pageContent = null;

		// Creating name value pair of user name and passsowrd
		List<NameValuePair> nvps = new ArrayList<NameValuePair>();
		nvps.add(new BasicNameValuePair("userlogin[login]", args
				.get(PLMConstants.USER_NAME)));
		nvps.add(new BasicNameValuePair("userlogin[password]", args
				.get(PLMConstants.PASSWORD)));
		pageContent = openPage(loginURL, nvps);

		logger.info("Exiting from execute");

		return pageContent;
	}
}
